<?php

namespace app\company\controller;

use app\common\exception\BaseException;
use app\common\model\Dealer as DealerModel;
use app\common\controller\CompanyController;
use app\common\model\SalesmanDealer;
use app\common\service\annotation\ControllerAnnotation;
use app\common\service\annotation\NodeAnnotation;
use app\company\validate\DealerValidate;
use app\Request;
use think\App;
use think\response\Json;

/**
 * @ControllerAnnotation(title="经销商管理")
 */
class Dealer extends CompanyController
{
    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model         = new DealerModel();
        $this->validateClass = DealerValidate::class;
        $this->sort          = ['sort' => 'desc', 'id' => 'desc'];
    }

    /**
     * @NodeAnnotation (title="新增")
     */
    public function add(Request $request): Json
    {
        try {

            $this->checkPost();
            $post = $request->post();
            $rule = [
                    'name|经销商名称'       => 'require|max:100|unique:dealer,name,company_id^delete_time',
                    'contact|联系人'        => 'require|max:100',
                    'phone|手机号'          => 'require|max:11',
//                    'service_rate|服务费率' => 'require',
                    'address|地址'          => 'require|max:100',
            ];
            $this->validate($post, $rule);
            $post['no'] = generateOrderNo('JXS');
            if (empty($post['password'])) $post['password'] = '123456';
            $post['password']   = password($post['password']);
            $post['company_id'] = $this->companyId;
            $this->model->transaction(function () use ($post, &$save)
            {
                $save = $this->model->save($post);
            });
        } catch (\Exception $e) {
            return $this->error('新增失败:' . $e->getMessage());
        }
        return $this->success('新增成功');
    }

    /**
     * @NodeAnnotation (title="编辑")
     * @throws BaseException
     */
    public function edit(Request $request): Json
    {
        try {
            $this->checkPost();
            $post = $request->post();
            $rule = [
                    'id' => 'require|integer'
            ];
            $this->validate($post, $rule);
            $row = $this->model->find($post['id']);
            if (!$row) return $this->error('数据不存在！');
            if (empty($post['password'])) $post['password'] = '123456';
            $post['password'] = password($post['password']);
            $this->model->transaction(function () use ($post, $row)
            {
                $row->save($post);
            });
        } catch (\Exception $e) {
            return $this->error('保存失败:' . $e->getMessage());
        }
        return $this->success('保存成功');
    }


    /**
     * @NodeAnnotation (title="修改密码")
     */
    public function password(Request $request, $id): Json
    {
        $row = $this->model->find($id);
        if (empty($row)) return $this->error('数据不存在');
        $post = $request->post();
        $this->sceneValidate($post, 'password');
        try {
            $this->model->transaction(function () use ($post, $row, &$save)
            {
                $save = $row->save(['password' => password($post['password'])]);
            });
        } catch (\Exception $e) {
            $this->error('操作失败:' . $e->getMessage());
        }
        if (!$save) return $this->error('操作成功');
        return $this->success('操作成功');
    }
}